package com.bestcem.xm.txtanls.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.txtanls.enums.ColumnDataTypeEnum;
import com.bestcem.xm.txtanls.enums.WorkTableVisibleEnum;
import com.bestcem.xm.txtanls.grpc.client.BaseBiGrpcService;
import com.bestcem.xm.txtanls.grpc.convert.ColumnConvert;
import com.bestcem.xm.txtanls.grpc.service.param.worktable.ListWorktableParam;
import com.bestcem.xm.txtanls.service.WorktableService;
import com.bestcem.xm.txtanls.service.dto.worktable.ColumnDTO;
import com.bestcem.xm.txtanls.service.dto.worktable.WorktableDTO;
import com.bestcem.xm.txtanls.service.dto.worktable.WorktableFromBIDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.bestcem.xm.common.core.constant.Constants.DEFAULT_ARRAY_SIZE;

/**
 * @author liheng
 * @version v1.0
 * @date 2021/5/11-10:51
 */

@Slf4j
@Service
public class WorktableServiceImpl implements WorktableService {

    @Resource
    private BaseBiGrpcService baseBiGrpcService;

    @Override
    public ServiceResult<WorktableFromBIDTO> getWorktable(String worktableId) {
        WorktableFromBIDTO workTableFromBIDTO = new WorktableFromBIDTO();
        // 请求python获取worktable信息
        WorktableDTO worktable = baseBiGrpcService.getWorktable(worktableId);
        if (Objects.isNull(worktable) || StringUtils.isAnyBlank(worktable.getId(), worktable.getName())) {
            // 查询失败
            return ServiceResult.fail(workTableFromBIDTO, "数据源不存在");
        }
        // 查询数据源所有列信息
        List<ColumnDTO> allfields = baseBiGrpcService.listColumnByWorktableId(worktableId, true);
        List<ColumnDTO> textFields = allfields.stream().filter(o -> ColumnDataTypeEnum.STRING.getIndex() == o.getDType()).collect(Collectors.toList());
        List<ColumnDTO> dateFields = allfields.stream().filter(o -> ColumnDataTypeEnum.DATETIME.getIndex() == o.getDType()).collect(Collectors.toList());

        workTableFromBIDTO.setId(worktableId);
        workTableFromBIDTO.setName(worktable.getName());
        if (CollUtil.isNotEmpty(textFields)) {
            workTableFromBIDTO.setTextFields(ColumnConvert.columnToFieldList(textFields));
        }
        if (CollUtil.isNotEmpty(dateFields)) {
            workTableFromBIDTO.setTimeFields(ColumnConvert.columnToFieldList(dateFields));
        }
        return ServiceResult.success(workTableFromBIDTO);
    }

    @Override
    public ServiceResult<List<WorktableFromBIDTO>> listWorktable(ListWorktableParam param) {
        ArrayList<WorktableFromBIDTO> worktableFromBIDTOS = new ArrayList<>(DEFAULT_ARRAY_SIZE);
        List<WorktableDTO> worktableDTOS = baseBiGrpcService.listWorktableByOrgIdAndUserId(param.getOrgId(), param.getUserId());
        if (CollUtil.isEmpty(worktableDTOS)) {
            return ServiceResult.success(worktableFromBIDTOS, "数据源信息为空");
        }

        for (WorktableDTO worktableDTO : worktableDTOS) {
            if (Objects.isNull(worktableDTO)) {
                continue;
            }
            if (WorkTableVisibleEnum.VISIBLE.getIndex() != worktableDTO.getVisible()) {
                continue;
            }
            if (ObjectUtils.isEmpty(worktableDTO.getId())) {
                log.error("[Txtanls] listWordTable返回部分worktableId为空; orgId:{}; userId:{}", param.getOrgId(), param.getUserId());
                continue;
            }
            WorktableFromBIDTO workTableFromBIDTO = new WorktableFromBIDTO();

            workTableFromBIDTO.setId(worktableDTO.getId());
            if (StringUtils.isNotBlank(worktableDTO.getName())) {
                workTableFromBIDTO.setName(worktableDTO.getName());
            }
            worktableFromBIDTOS.add(workTableFromBIDTO);
        }
        return ServiceResult.success(worktableFromBIDTOS);
    }
}
